package com.songcha.library_network.api.factory


import com.google.gson.Gson
import com.google.gson.TypeAdapter
import okhttp3.ResponseBody
import retrofit2.Converter
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets.UTF_8


class GsonResponseBodyConverter<T>(val gson: Gson,val adapter:TypeAdapter<T>):Converter<ResponseBody,T> {
    override fun convert(value: ResponseBody): T? {
            //json字符串
            val response=value.string()

            val mediaType=value.contentType()
            val charset= if (mediaType != null) mediaType.charset(UTF_8) else UTF_8
            val inputStream=ByteArrayInputStream(response.toByteArray())
            val jsonReader=gson.newJsonReader(InputStreamReader(inputStream,charset))

            return adapter.read(jsonReader)

    }

}

/*class NetWorkException(throwable: Throwable):Exception(throwable)

class ApiCodeException(val code:Int,val msg:String):Exception()

class LoginExpireException:Exception()*/

